package com.liyujie.splitscreen.ui.home;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;


import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.ToastUtils;
import com.liyujie.serialport.utils.OpenSerialportUtils;
import com.liyujie.splitscreen.R;
import com.liyujie.splitscreen.databinding.ActivityHomeBinding;
import com.liyujie.splitscreen.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends BaseActivity<ActivityHomeBinding> {

    private DifferentDisplay mPresentation;

    private int location;

    @Override
    public void initCreate() {
        super.initCreate();
        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {

        return R.layout.activity_home;
    }

    @Override
    public void initView() {
        super.initView();
        /**
         * 初始化串口
         */
        OpenSerialportUtils.initializationSerialPort();
        /**
         * 打开开门串口
         */
        OpenSerialportUtils.openOpenSerialPort("/dev/ttyS4");

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            //启动Activity让用户授权
            if (!Settings.canDrawOverlays(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse( "package:com.liyujie.splitscreen"));
                startActivityForResult(intent, 1010);
                return;
            }
        }
    }


    @Override
    public void initData() {
        super.initData();
        if (mPresentation == null) {
            DisplayManager mDisplayManager;// 屏幕管理类
            mDisplayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
            Display[] displays = mDisplayManager.getDisplays();

            mPresentation = new DifferentDisplay(this, displays[displays.length - 1]);// displays[1]是副屏
            mPresentation.show();
        }
    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        binding.ButtonUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != mPresentation){
                    List<String> stringList = new ArrayList<>();
                    for (int i = 0;i < 100; i++){
                        location ++;
                        stringList.add(location +"");
                    }
                    mPresentation.setStringList(stringList);
                }
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10110) {
            if (!Settings.canDrawOverlays(this)) {
                ToastUtils.make().show("授权失败");
            } else {
                ToastUtils.make().show( "授权成功");
                initData();
            }
        }
    }

}